package com.wjd.wshow.config;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

@Component
public class ContextHolder implements ApplicationContextAware {

    private static ContextHolder instance = new ContextHolder();

    private static ApplicationContext context = null;

    private ContextHolder() {}

    public static ContextHolder getInstance() {
        return instance;
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        context = applicationContext;
    }

    /**
     * 通过名称获取指定对象
     * @param name 名称
     * @return 类实例
     */
    public Object getBean(String name) {
        return context.getBean(name);
    }

    /**
     * 通过类获取指定对象
     * @param clazz 类对象
     * @param <T> 类型
     * @return 类实例
     */
    public <T> T getBean(Class<T> clazz) {
        return context.getBean(clazz);
    }
}
